package com.prim.update.service;

import android.app.IntentService;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.prim.update.UpdateManager;
import com.prim.update.callback.DownloadCallback;

/**
 * ================================================
 * 作    者：linksus
 * 版    本：1.0
 * 创建日期：3/6 0006
 * 描    述：下载的服务类
 * 修订历史：
 * ================================================
 */
public class UpdateDownloadService extends IntentService {
    private static final String ACTION_FOO = "com.su.download_library.service.action.FOO";

    private static final String EXTRA_PARAM1 = "downUrl";
    private static final String EXTRA_PARAM2 = "path";

    private static DownloadCallback downloadCallback;

    public UpdateDownloadService() {
        super("UpdateDownloadService");
    }


    /**
     * 开始服务
     *
     * @param context
     * @param downUrl
     *         下载地址
     * @param path
     *         下载保存的路径
     */
    public static void startActionFoo(Context context, DownloadCallback callback, String downUrl, String path) {
        downloadCallback = callback;
        Intent intent = new Intent(context, UpdateDownloadService.class);
        intent.setAction(ACTION_FOO);
        intent.putExtra(EXTRA_PARAM1, downUrl);
        intent.putExtra(EXTRA_PARAM2, path);
        context.startService(intent);
    }

    private final static int GRAY_SERVICE_ID = 1001;

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        /**
         * 实现下载服务的灰度保活
         */
        if (Build.VERSION.SDK_INT < 18) {
            startForeground(GRAY_SERVICE_ID, new Notification());//API < 18 ，此方法能有效隐藏Notification上的图标
        } else {
            Intent innerIntent = new Intent(this, GrayInnerService.class);
            startService(innerIntent);
            startForeground(GRAY_SERVICE_ID, new Notification());
        }
        onHandleIntent(intent);
        return START_STICKY;
    }

    /**
     * 给 API >= 18 的平台上用的灰度保活手段
     */
    public class GrayInnerService extends Service {

        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            startForeground(GRAY_SERVICE_ID, new Notification());
            stopForeground(true);
            stopSelf();
            return super.onStartCommand(intent, flags, startId);
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }

    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            if (ACTION_FOO.equals(action)) {
                final String downUrl = intent.getStringExtra(EXTRA_PARAM1);
                final String path = intent.getStringExtra(EXTRA_PARAM2);
                handleActionFoo(downUrl, path);
            }
        }
    }

    /**
     * Handle action Foo in the provided background thread with the provided
     * parameters.
     */
    private void handleActionFoo(String downUrl, String path) {
        //开启下载
        UpdateManager.getInstance().getHttpClient().download(downUrl, path, downloadCallback);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
